clear all; 
% clc;

addpath(genpath('gpml-matlab-v3.1-2010-09-27'), '-end');
addpath(genpath('Netlab'), '-end');
addpath(genpath('utils'), '-end');


%rand('seed', 12);
%randn('seed', 24);

cov_func = 'covSEiso';
n_expts = 5;
hyp_mean = [0.6;0.6];
n_class = 3;


[x, y1, n_class, Hyps] = generate_data(hyp_mean, cov_func, n_class); % or whatever
% x = normalise(x);

n   = size(x,1);
eval(['dim =', 'str2num(feval(cov_func))', ';']); 
ntot = n;
hyps = reshape(Hyps, 1, n_class*dim);


%
%y1  = randi(n_class, n, 1);
I = eye(n_class);
Y = I(y1,:);
y = Y(:);

KK = zeros(n,n,n_class);
sigma_noise = 1e-7;
for c = 1:n_class
  KK(:,:,c) = feval(cov_func,hyps(c*2-1:c*2), x, x) + sigma_noise*eye(n); % hax
end  
approxF = alg_3_3(n, n_class, KK, y);
dels = zeros(n_expts, dim*n_class);

approxF = zeros(size(approxF));
%approxF = repmat(rand(n, 1), n_class, 1);

%% Here we test the computation of log_det_B
% %Hyps = repmat(rand(dim,1), 1, n_class); % same hyper across all classes
Hyps = rand(dim, n_class); % different hyper across all classes
% %
hyps = reshape(Hyps, 1, n_class*dim);
% log_det_B(hyps, cov_func, n, n_class, x, y, approxF);
% return;


for gci = 1:n_expts
  %hyps = repmat([0.2 * (0.5 * (n_expts + 1) - gci); 0], n_class, 1)';
  hyps = repmat(rand(1, dim), 1, n_class);
  fprintf('Performing gradchek %i\n', gci);
  [grad, del] = gradchek(hyps, @log_det_B, @del_log_det_B, cov_func, n, n_class, x, y, approxF);
  dels(gci, :) = del;
end
clf;
hist(dels(:));


   